package com.ithiema.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.dto.SetmealDto;
import com.ithiema.reggie.pojo.Category;
import com.ithiema.reggie.pojo.Dish;
import com.ithiema.reggie.pojo.Setmeal;
import com.ithiema.reggie.service.CategoryService;
import com.ithiema.reggie.service.SetmealDishService;
import com.ithiema.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    //增
    @Cacheable(value = "setmealCache")
    @PostMapping
    public R<String>save( @RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
return R.success("新增套餐成功");

    }


    //分页查询
    @GetMapping("/page")

    public R<Page> page(Integer page,Integer pageSize,String name){
        //分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，模糊查询
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //排序条件，降序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行枫叶
        setmealService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }
    //删
    @Cacheable(value = "setmealCache",key = "#id")
    @DeleteMapping
    public R<String>delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> querySetmealWithDishesById(@PathVariable Long id){
        log.info("根据id获取套餐信息,id为:{id}",id);
        SetmealDto setmealDto = setmealService.getByIdWithDishes(id);
        if (setmealDto == null) {
            return R.error("未查询到该套餐信息");
        }
        return R.success(setmealDto);
    }
    @Cacheable(value = "setmealCache",key = "#id")
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        log.info("修改套餐，数据为:{}",setmealDto);
        setmealService.updateSetmealWithDishes(setmealDto);
        return R.success("成功修改套餐信息");

    }

    @Cacheable(value = "setmealCache",key = "#id")
    @PostMapping("/status/{statusCode}")
    public R<String> switchStatus(@PathVariable int statusCode,@RequestParam List<Long> ids){
        log.info("更改销售状态");
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Setmeal::getStatus,statusCode);
        wrapper.in(Setmeal::getId,ids);
        setmealService.update(wrapper);
        return R.success("成功更改销售状态");
    }
@Cacheable(value = "setmealCache",key = "'setmeal_'+#root.args[0]",unless = "#result.data.size()==0")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);

    }


}

